﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GsnHelper;
using PetaPoco;

namespace ZhongheCms.Models
{
    public partial class GswTblContent
    {
        [ResultColumn]
        public List<ProMasterType> ProTypes { get; set; }
         [ResultColumn]
        public List<GswTblContent> ContentList0 { get; set; }
        [ResultColumn]
        public List<GswTblContent> ContentList1 { get; set; }
        [ResultColumn]
        public List<GswTblJobInfo> JobInfos { get; set; }
        public static Page<GswTblContent> GetContentList(string sdate, string edate, string title, string funcId, int cusid,int pageIndex)
        {
            //ContentType
            sdate = string.IsNullOrWhiteSpace(sdate) ? "" : Convert.ToDateTime(sdate).ToShortDateString();
            edate = string.IsNullOrWhiteSpace(edate) ? "" : Convert.ToDateTime(edate).ToShortDateString();
            var strSql = "select * from GswTblContent where FunctionId='" + funcId + "' and CustomerId=" + cusid;

            if (!string.IsNullOrWhiteSpace(title))
            {
                strSql += " and  ContentTitle like '%" + title + "%' ";
            }
            if (InputValid.IsDate(sdate))
            {
                strSql += " and CreateDate>='" + sdate + "'";
            }
            if (InputValid.IsDate(edate))
            {
                strSql += " and CreateDate<='" + edate + "'";
            }

            strSql += " order by Id desc";
            var db = new ZhongheRepo();
            var qry = db.Page<GswTblContent>(pageIndex, 20, strSql);
            return qry;
        }

        public static List<GswViewContent> GetSchoolShare(string sdate, string edate, string title, string funcId, int cusid)
        {
            //ContentType
            sdate = string.IsNullOrWhiteSpace(sdate) ? "" : Convert.ToDateTime(sdate).ToShortDateString();
            edate = string.IsNullOrWhiteSpace(edate) ? "" : Convert.ToDateTime(edate).ToShortDateString();
            var strSql = "select * from GswViewContent where FunctionId='" + funcId + "' and CustomerId=" + cusid;

            if (!string.IsNullOrWhiteSpace(title))
            {
                strSql += " and  ContentTitle like '%" + title + "%' ";
            }
            if (InputValid.IsDate(sdate))
            {
                strSql += " and CreateDate>='" + sdate + "'";
            }
            if (InputValid.IsDate(edate))
            {
                strSql += " and CreateDate<='" + edate + "'";
            }

            strSql += " order by Id desc";

            return GswViewContent.Fetch(strSql);
        }
        public static List<GswTblContent> GetContentList(string title, string funcId, int cusid)
        {
            //ContentType
         
            var strSql = "select * from GswTblContent where FunctionId='" + funcId + "' and CustomerId=" + cusid;

            if (!string.IsNullOrWhiteSpace(title))
            {
                strSql += " and  ContentTitle like '%" + title + "%' ";
            }

            strSql += " order by ContentNo";

            return Fetch(strSql);
        }
        public static List<ProMasterType> GetShareTypes(int cusid)
        {
            var fid = "86";

            var strSql = "select Id,MasterType from GswTblBaseInfo where MasterId=0 and FunctionId=" + fid + " and CustomerId=" + cusid + " order by TypeNo";
            var mType = ZhongheRepo.GetInstance().Fetch<ProMasterType>(strSql);
            //var subTypeExiste = GswTblBaseInfo.FirstOrDefault("where MasterId>0 and CustomerId=" + cusid + " and  FunctionId=" + 37);
            //if (subTypeExiste == null) return mType;
            foreach (var m in mType)
            {
                m.SubFlag = 1;
                strSql = "select Id,SubType from GswTblBaseInfo where MasterId=" + m.Id + " Order By TypeNo";
                m.ProSubTypes = ZhongheRepo.GetInstance().Fetch<ProSubType>(strSql);
            }

            return mType;

        }
    }
}